
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpregistryBO;
import com.wyzz.hr.dao.IEmpregistryDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empregistry;


public class EmpregistryBOImpl implements IEmpregistryBO {

	public EmpregistryBOImpl() {
	}

	public IEmpregistryDAO getEmpRegistryDAO() {
		return empRegistryDAO;
	}

	public void setEmpRegistryDAO(IEmpregistryDAO empRegistryDAO) {
		this.empRegistryDAO = empRegistryDAO;
	}

	public Empregistry loadById(String id, String fetch[]) {
		return (Empregistry) empRegistryDAO.loadObject(
				com.wyzz.hr.pojo.Empregistry.class, id, fetch,
				new boolean[] { false });
	}

	public List loadByEmp(String empId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empregistry.class);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Empregistry.PROP_EREG_EMP_NO).append(".").append(
				Employee.PROP_ID).toString(), empId));
		List result = empRegistryDAO.findByCriteria(dc);
		if (result.isEmpty())
			return null;
		else
			return result;
	}

	public String insert(Empregistry empRegistry) {
		empRegistryDAO.saveObject(empRegistry);
		return empRegistry.getEregId();
	}

	public void delete(Empregistry empRegistry) {
		empRegistryDAO.deleteObject(empRegistry);
	}

	public void update(Empregistry empRegistry) {
		empRegistryDAO.updateObject(empRegistry);
	}

	public boolean deleteAttach(String eregId, String fileName) {
		try {
			FileOperate.deleteFile("sys.profile.file.path", fileName);
			String hql = (new StringBuilder())
					.append(
							"update Empregistry set eregAttachment=null where eregId='")
					.append(eregId).append("'").toString();
			empRegistryDAO.exeHql(hql);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	private IEmpregistryDAO empRegistryDAO;
}
